package cn.quang.search.toolkit.component.items.impl;

import cn.quang.search.toolkit.component.AbstractSearchItem;
import cn.quang.search.toolkit.component.constants.Constants;
import cn.quang.search.toolkit.enums.DataSourceTypeEnum;
import cn.quang.search.toolkit.enums.SearchOperator;
import cn.quang.search.toolkit.provider.entity.KeyOperatorValue;
import cn.quang.search.toolkit.provider.entity.QuerySlice;

import java.util.Collections;

/**
 * @Author: chenwenshuo
 * @Date: 2021/11/06/6:10 下午
 */
public class LikeSearchItem extends AbstractSearchItem {
    private final String  value;

    public <T> LikeSearchItem(DataSourceTypeEnum dsType, String col, SearchOperator operator, T value) {
        super(dsType, col, operator);
        if (operator == SearchOperator.Like){
            this.value = "%"+value+"%";
            return;
        }
        if (operator == SearchOperator.LeftLike){
            this.value = "%"+value;
            return;
        }
        if (operator == SearchOperator.RightLike){
            this.value = value+"%";
            return;
        }
        this.value="";
    }

    @Override
    public QuerySlice getQuerySlice() {
        String sqlTemplate = Constants.FIELD + " " + Constants.OPERATOR + " " + Constants.VALUE;
        return QuerySlice.of(
                sqlTemplate,
                KeyOperatorValue.of(col, Collections.singletonList(col), Collections.singletonList(operator.getOperator()), Collections.singletonList(value))
        );
    }
}
